package com.gzxx.security;

import com.gzxx.util.GsonUtil;
import com.gzxx.util.ServerResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Pengpdx
 * @description: 退出操作
 * @date 2020/9/16 18:37
 */
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserLogoutSuccessHandler implements LogoutSuccessHandler {

    private final TokenService tokenService;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //清除Token
        tokenService.removeToken(request);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        String json = GsonUtil.toJson(ServerResponse.success());
        response.getWriter().write(json);
    }
}
